#!/bin/sh
# 

# Construct paths
BASE=$(cd $(dirname $0); pwd)
SYSLAB_BASE=../syslab/
CP="$CP:lib/"
CP="$CP:bin/"
CP="$CP:lib/calimero-2.0.4-modified.jar"
CP="$CP:lib/batik-1.7.jar"
CP="$CP:lib/jssc.jar"
CP="$CP:lib/glpk-java-4.47.jar"
CP="$CP:lib/Jama-1.0.2.jar"
CP="$CP:lib/junit.jar"
CP="$CP:$SYSLAB_BASE/bin/"
CP="$CP:$SYSLAB_BASE/java/lib/xml-apis-ext-1.3.jar"
CP="$CP:$SYSLAB_BASE/java/lib/xmlrpc-2.0.1.jar:$SYSLAB_BASE/java/lib/jamod.jar"
CP="${CP}:$SYSLAB_BASE/java/lib/jade-3.7.jar:$SYSLAB_BASE/java/lib/iiop.jar:$SYSLAB_BASE/java/lib/http.jar"
CP="${CP}:$SYSLAB_BASE/java/lib/commons-codec.jar:$SYSLAB_BASE/java/lib/js.jar"


# Determine architecture for use with binary executeables
ARCH=x86
case "$(uname -m)" in
  x86_64) ARCH=x86_64;;
  i686) ARCH=x86;;
  i586) ARCH=x86;;
  *) echo "WARNING: Unknown architecture: $(uname -m)";;
esac
LP="lib:lib/jni/linux-$ARCH"


if test -z "$1" || test "$1" = "-l"; then
  ( cd src/; grep -l -r "public static void main" . | sed -e "s!/!.!g" -e 's/\.java$//' -e "s/^\\.\\.//" -e "s/^/ /" | sort )
  exit 1
fi

# When the class is given as a file name, that is converted into a class name by:
# * removing leading "java/src/", if present
# * removing trailing  ".java", if present
# * replacing any "/" chars with "."
CLASS=$(echo $1 | sed -e "s%^src/%%" -e "s%^bin/%%" -e "s%\\.java\$%%" -e "s%\\.class\$%%" -e "s%/%.%g")
shift

exec java $JAVA_ARGS -classpath "$CP" -Djava.library.path="$LP" -Dbase="$BASE" "$CLASS" "$@"

